import os
import re
from tkinter import NORMAL

import pyautogui
import ttkbootstrap as ttk
import tkinter as tk
from tkinter import filedialog
import variable
import SoftWareUpdate as swu


def get_gui():
    top1 = tk.Toplevel(container=False, borderwidth=50)
    top1.title('请输入用户名和密码')

    def get_install_path():
        install_path = tk.filedialog.askopenfilename()
        entry_path.set(install_path)

    # 退出界面
    def Quit_install():

        top1.destroy()

    # 确认按钮 安装软件
    def Start_install():
        install_path = entry_path.get()
        install_path1=install_path.replace(r"/","\\")
        install_url = entry_url.get()
        pwd = entry_pwd.get()
        if pwd == variable.ADMIN_PASSWORD:
            if install_url:
                swu.SoftWareUpdate(install_url).download_software()
            else:
                print(install_path1)
                swu.SoftWareUpdate().execution_software(software_path=install_path1)
        else:
            pyautogui.alert(title='报错', text='密码错误')

        # t = os.system(
        #     variable.PsExec_Path + ' -u ' + variable.USERNAME_PC000 + ' -p ' + str(pwd) + ' "' + install_path + '"')
        # if t == 1326:
        #     os.system(
        #         variable.PsExec_Path + ' -u ' + variable.USERNAME_SYSADMIN + ' -p ' + str(
        #             pwd) + ' "' + install_path + '"')
        top1.destroy()

    # 文本框
    entry_path = tk.StringVar()
    entry_url = tk.StringVar()
    entry_pwd = tk.StringVar()

    # 路径
    ttk.Button(master=top1, bootstyle="success", text='选择路径', command=get_install_path).grid(row=1, column=1)
    tk.Entry(master=top1, state=NORMAL, textvariable=entry_path, ).grid(row=1, column=2)

    #URL路径
    # tk.Label(master=top1, text='URL路径', justify='left').grid(row=2, column=1)
    # tk.Entry(master=top1, width=20, textvariable=entry_url, ).grid(row=2, column=2)

    # 密码
    tk.Label(master=top1, text='管理员密码', justify='left').grid(row=3, column=1)
    tk.Entry(master=top1, width=20, textvariable=entry_pwd, show='*', ).grid(row=3, column=2)

    tk.Button(master=top1, text='确认', command=Start_install).grid(row=4, column=1)
    tk.Button(master=top1, text='退出', command=Quit_install).grid(row=4, column=2)
